package com.example.consumptionrecords.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.consumptionrecords.entity.pojo.User;
import com.example.consumptionrecords.entity.pojo.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 孙敬佳
 * 2024 \ 02 \ 06
 */

@Mapper
public interface UserMapper extends BaseMapper<User> {

	/**
	 * 根据用户名或手机号查询用户
	 * @param username 用户名或手机号
	 * @return 用户
	 */
	User selectUserByUsernameOrPhone(@Param(value = "username")String username);

	/**
	 * 获取用户消费总额
	 * @param userId 用户id
	 * @return 消费总额
	 */
	@Select("SELECT IFNULL(SUM(con_amount), 0) FROM con_records WHERE user_id = #{userId}")
	String getTotalConsumption(@Param(value = "userId")Integer userId);

	/**
	 * 用户名是否存在
	 * @param userName 用户名
	 * @return 大于零false, 等于零true
	 */
	@Select("SELECT COUNT(*) FROM u_user WHERE user_name = #{userName}")
	Boolean userNameIsExists(@Param(value = "userName")String userName);

	/**
	 * 手机号是否存在
	 * @param userName 手机号
	 * @return 大于零false, 等于零true
	 */
	@Select("SELECT COUNT(*) FROM u_user WHERE user_phone = #{userName} AND delete_status = 0")
	Boolean userPhoneIsExists(@Param(value = "userName")String userName);

	/**
	 * 插入用户
	 * @param user 用户
	 */
	void insertUser(@Param(value = "user") User user);

	/**
	 * 根据用户id查询用户信息
	 * @param userId 用户id
	 * @return 用户信息
	 */
	UserInfo selectUserInfoByUserId(@Param(value = "userId")Integer userId);
}
